﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LotteryAdmin.AdminService;
using System.Windows.Data;

namespace LotteryAdmin
{
    public partial class UserProfitLoss : UserControl
    {
        private LotteryAdminClient srv;
        DateTime startDate
        {
            get { return datePickerStart.SelectedDate.Value.Date; }
            set { datePickerStart.SelectedDate = value; }
        }
        DateTime endDate
        {
            get { return datePickerEnd.SelectedDate.Value.Date.AddDays(1).AddSeconds(-1); }
            set { datePickerEnd.SelectedDate = value; }
        }
        public UserProfitLoss()
        {
            InitializeComponent();
            this.datePickerStart.SelectedDate = DateTime.Now;
            this.datePickerEnd.SelectedDate = DateTime.Now;
            this.Loaded += new RoutedEventHandler(UserProfitLoss_Loaded);
        this.KeyDown += new KeyEventHandler(Page_KeyDown);
        }
        void Page_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.PlatformKeyCode == 13)
                this.btnSearch_Click(this.btnSearch, null);
        }

        void UserProfitLoss_Loaded(object sender, RoutedEventArgs e)
        {
            LoadStatis();
        }
        void LoadStatis()
        {
            this.sp_Type.Visibility = Visibility.Collapsed;
            srv = WcfProxy.GetProxy;
            loadingProgress.IsBusy = true;
            srv.GetProfitReportAsync(txtUserID.Text.ToString().Trim(), startDate, endDate);
            srv.GetProfitReportCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                if (e.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                PagedCollectionView pcv = new PagedCollectionView(e.Result);
                this.dp_Statis.Source = pcv;
                this.dgStatis.ItemsSource = pcv;
            };
        }
        void LoadDetail()
        {
            this.sp_Type.Visibility = Visibility.Visible;
            string type = (cbxType.SelectedItem as ComboBoxItem).Content.ToString() == "请选择" ? "" :
                (cbxType.SelectedItem as ComboBoxItem).Content.ToString();
            srv = WcfProxy.GetProxy;
            srv.GetProfitDetailAsync(txtUserID.Text.ToString().Trim(), type, startDate, endDate);
            loadingProgress.IsBusy = true;
            srv.GetProfitDetailCompleted += (s, e) =>
            {
                loadingProgress.IsBusy = false;
                if (e.Result.Count == 0)
                {
                    MessageBox.Show("没有找到匹配的记录!");
                }
                PagedCollectionView pcv = new PagedCollectionView(e.Result);
                this.dp_Detail.Source = pcv;
                this.dgDetail.ItemsSource = pcv;
            };
        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (!Validate.ValidateUserID(this.txtUserID.Text))
            {
                MessageBox.Show("用户名只能输入英文、汉字、数字、下划线");
                return;
            }
            LoadData();
        }
        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadData();
        }
        void LoadData()
        {
            this.UpdateLayout();
            if (tabItem0 != null && tabItem0.IsSelected)
                LoadStatis();
            if (tabItem1 != null && tabItem1.IsSelected)
                LoadDetail();
        }
        private void dgStatis_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgStatis.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dp_Statis.PageSize * dp_Statis.PageIndex+e.Row.GetIndex() + 1).ToString();
            (this.dgStatis.Columns[7].GetCellContent(e.Row) as Button).Tag = e.Row.DataContext;
        }

        private void dgDetail_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            (this.dgDetail.Columns[0].GetCellContent(e.Row) as TextBlock).Text = (dp_Detail.PageSize * dp_Detail.PageIndex + e.Row.GetIndex() + 1).ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            txtUserID.Text = ((sender as Button).Tag as ProfitReport).UserID;
            this.tabItem1.IsSelected = true;
            LoadDetail();
        }
    }
}
